
package com.cskaoyan.controller.wx.order;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.GenericData;
import com.cskaoyan.bean.po.MarketOrder;
import com.cskaoyan.bean.po.MarketOrderGoods;
import com.cskaoyan.bean.wx.order.OrderCommentBo;
import com.cskaoyan.bean.wx.order.OrderDetailVo;
import com.cskaoyan.bean.wx.order.WxMarketOrderVo;
import com.cskaoyan.bean.wx.order.WxOrderSubmitBo;
import com.cskaoyan.bean.wx.order.WxOrderSubmitVo;
import com.cskaoyan.config.exception.LoginAuthenException;
import com.cskaoyan.service.wx.WxOrderService;
import com.cskaoyan.util.UserIdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Map;

/**
 * @author LiangRuiJiang
 * @since 2022/06/08  14:19
 */
@RestController
@RequestMapping("wx/order")
public class WxOrderController {

    @Autowired
    WxOrderService wxOrderService;

    /**
     * 订单列表
     * @param showType
     * @param page
     * @param limit
     * @return
     * @throws LoginAuthenException
     */
    @RequestMapping("list")
    public BaseRespVo list(Short showType,Integer page,Integer limit) throws LoginAuthenException {
        Integer userId = UserIdUtil.getUserIdFromSubjectThrowException();

        GenericData<WxMarketOrderVo> marketOrderVo =  wxOrderService.queryOrderList(showType,page,limit,userId);
        return BaseRespVo.ok(marketOrderVo);
    }

    /**
     * 订单详情
     * @param orderId
     * @return
     * @throws LoginAuthenException
     */
    @RequestMapping("detail")
    public BaseRespVo detail(Integer orderId) throws LoginAuthenException {
        Integer userId = UserIdUtil.getUserIdFromSubjectThrowException();
        OrderDetailVo orderDetailVo = wxOrderService.queryOrderDetail(orderId,userId);

        return BaseRespVo.okAndSuccess(orderDetailVo);
    }

    /**
     * 申请退款
     * @param map
     * @return
     */
    @PostMapping("refund")
    public BaseRespVo refund(@RequestBody Map map, HttpServletRequest request){
        Integer orderId = (Integer) map.get("orderId");
        String executionMethod = getExecutionMethod(request);
        wxOrderService.UpdateOrderStatus(orderId,executionMethod);
        return BaseRespVo.ok();
    }

    private String getExecutionMethod(HttpServletRequest request) {
        String uri = request.getRequestURI();
        Class<WxOrderController> orderControllerClass = WxOrderController.class;
        Method[] methods = orderControllerClass.getMethods();
        String executionMethod = null;
        for (Method method : methods) {
            String methodName = method.getName();
            if(uri.contains(method.getName())){
                executionMethod = methodName;
            }
        }
        return executionMethod;
    }

    /**
     * 确认收货
     * @param map
     * @return
     */
    @PostMapping("confirm")
    public BaseRespVo confirm(@RequestBody Map map,HttpServletRequest request){
        Integer orderId = (Integer) map.get("orderId");
        String executionMethod = getExecutionMethod(request);
        wxOrderService.UpdateOrderStatus(orderId,executionMethod);
        // wxOrderService.createAfterSaleRecord(orderId);
        return BaseRespVo.ok();
    }

    /**
     * oushao 2022-06-09    17:10
     * @return
     */
    @PostMapping("submit")
    public BaseRespVo submit(@RequestBody() WxOrderSubmitBo wxOrderSubmitBo) throws LoginAuthenException {

        BaseRespVo baseRespVo = wxOrderService.insertOrder(wxOrderSubmitBo);

        return baseRespVo;
    }

    /**
     * oushao 2022-06-09   22:26
     * @param map
     * @return
     */
    @PostMapping("prepay")
    public BaseRespVo prepay(@RequestBody Map map){
        Integer orderId = 0;
        try{
            orderId = (Integer) map.get("orderId");
        }catch (Exception e){
            String id = (String) map.get("orderId");
            orderId = Integer.valueOf(id);
        }

        MarketOrder marketOrder = new MarketOrder();

        marketOrder.setId(orderId);

        wxOrderService.update(marketOrder);

        return BaseRespVo.ok();
    }

    /**
     * 商品订单回显
     * @param orderId
     * @param goodsId
     * @return
     */
    @RequestMapping("goods")
    public BaseRespVo goods(Integer orderId,Integer goodsId){
        MarketOrderGoods marketOrderGoods = wxOrderService.queryOrderGoods(orderId,goodsId);


        return BaseRespVo.ok(marketOrderGoods);
    }

    /**
     * 订单评论
     * @param orderCommentBo
     * @return
     * @throws LoginAuthenException
     */
    @RequestMapping("comment")
    public BaseRespVo comment(@RequestBody() OrderCommentBo orderCommentBo) throws LoginAuthenException {
        Integer userId = UserIdUtil.getUserIdFromSubjectThrowException();
        wxOrderService.createOrderComment(orderCommentBo,userId);

        return BaseRespVo.ok();
    }

    /**
     * 删除订单
     * @param map
     * @return
     */
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody() Map map){
        Integer orderId = (Integer) map.get("orderId");
        wxOrderService.deleteOrder(orderId);
        return BaseRespVo.ok();
    }

    @PostMapping("cancel")
    public BaseRespVo cancel(@RequestBody() Map map){
        Integer orderId = (Integer) map.get("orderId");
        wxOrderService.cancelOrder(orderId);
        return BaseRespVo.ok();
    }

}

